﻿using System.Security.Cryptography;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;

namespace WebApi.utils;

public class TokenUtils
{
    
    public static  IJwtEncoder Encoder { get; }

    public static IJwtDecoder Decoder { get; }
    
    
    static TokenUtils()
    {
        // 创建 RSA 实例
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            // 生成密钥对
            RSAParameters publicKey = rsa.ExportParameters(false); // 公钥
            RSAParameters privateKey = rsa.ExportParameters(true); // 私钥

            
            
            // 这里可以使用 publicKey 和 privateKey 进行后续操作
            // ...
            

            IJwtAlgorithm algorithm = new RS256Algorithm(RSA.Create(publicKey),RSA.Create(privateKey));
            IJsonSerializer serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            Encoder  = new JwtEncoder(algorithm, serializer, urlEncoder);
            
            IDateTimeProvider provider = new UtcDateTimeProvider();
            
            IJwtValidator validator = new JwtValidator(serializer, provider);
            
            Decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);

        }
    }
}